<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/20
 * Time: 18:24
 */

namespace app\home\controller;

use app\common\controller\Backend;
use app\home\model\NeedInfo;
use app\home\model\RecordPlan;
use app\home\model\SystemLog;
use think\Db;
/**
 * @title 增删改需求
 * @description 接口说明
 * @group 后台管理
 */
class ProjectNeed extends Backend
{
    protected $hsModel = 'ProjectNeed';
    public $statusCode = [
        0  => '筹备中',
        1  => '待开发',
        2  => '开发中',
        3  => '待测试',
        4  => '测试中',
        5  => '待上线',
        6  => '已上线'
    ];
    //记录日记的字段
    protected $projectName = [
        'project_id' => [
            'name' => '所属项目',
            'table' => 'project'
        ],
        'product_id' => [
            'name' => '所属产品',
            'table' => 'project'
        ],
        'versions_id' => [
            'name' => '所属版本',
            'table' => 'versions'
        ],
        'block_id' => [
            'name' => '所属模块',
            'table' => 'block'
        ],
        'name' => [
            'name' => '所属模块',
            'table' => ''
        ],
        'child_block' => [
            'name' => '子模块',
            'table' => ''
        ],
        'type' => [
            'name' => '需求类型',
            'table' => 'deploy'
        ],
        'describe' => [
            'name' => '需求描述',
            'table' => ''
        ],
        'source' => [
            'name' => '需求来源',
            'table' => 'deploy'
        ],
        'priority' => [
            'name' => '优先级',
            'table' => 'deploy'
        ],
        'front_user' => [
            'name' => '指定前端',
            'table' => 'user'
        ],
        'after_user' => [
            'name' => '指定后端',
            'table' => 'user'
        ],
        'remark' => [
            'name' => '备注',
            'table' => ''
        ],
        'front_adjust' => [
            'name' => '前端用时调整',
            'table' => ''
        ],
        'after_adjust' => [
            'name' => '后端用时调整',
            'table' => ''
        ],
        'etime' => [
            'name' => '截至日期',
            'table' => ''
        ],
        'ptime' => [
            'name' => '提出日期',
            'table' => ''
        ],
        'status' => [
            'name' => '',
            'table' => '',
            'option' => 2
        ],
        'plan' => [
            'name' => '更新进度',
            'table' => ''
        ]
    ];

    /**
     * @title 查看需求
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/query/:id
     * @method POST
     */
    public function query($id)
    {
        if ( $this->request->isPost() ) {
            $map = $this->request->post();
            $page = (int)input('page', 1);
            $limit = (int)input('limit', 10);
            // 判断是否有条件
            if ( empty($map) ) {
                // 查询当前需求数据
                $data = Db::name($this->hsModel)->where('id',$id)->find();
                // 查询当前需求的图片 文件等
                $data['img'] = Db::name('NeedInfo')->where('needs_id',$id)->select();
                // 判断前后端
                if (USER_DEPT == 0) {
                    $prefix = 'front_';
                } else if (USER_DEPT == 1) {
                    $prefix = 'after_';
                }
                // 获取当前需求下关联的bug
                $data['bug'] = \app\home\model\ProjectBug::where('need_id',$data['id'])->field('id,title,describe,status')->order('ctime', 'desc')->select();
                $data['role'] = USER_DEPT;
                // 判断$prefix是否定义
                if (isset($prefix)) {
                    // 定义则循环加权限按钮
                    $data['isshow'] = $data[$prefix.'predict'] > 0 ? false : true;
                    $data['isself'] = $data[$prefix.'user'] == USER_ID ? true : false;
                    $data['isfinish'] = $data[$prefix.'plan'] == 100 ? false : true;
                }
                $data['time'] = $data['ptime'] ? date('Y-m-d',$data['ptime']) : '';

                $data['endtime'] = $data['etime'] ? date('Y-m-d',$data['etime']) : '';
            } else {
                // 有$map条件 就是筛选
                $where = [];
                $like = '';
                $issue_id = isset($map['issue_id']) && $map['issue_id'] == '' ? true : false;
                foreach ($map as $key => $value) {
                    if ($value === '') unset($map[$key]);
                }
                $where['task_allot'] = ['in',[0,4]];
                if (isset($map['project_id'])) {
                    $where['pn.project_id'] =  $map['project_id'];
                    if (isset($map['issue_id'])) $where['pn.issue_id'] = $map['issue_id'];
                } else if (isset($map['product_id'])) {
                    $where['pn.product_id'] = $map['product_id'];
                    if (isset($map['versions_id'])) $where['pn.versions_id'] =  $map['versions_id'];
                } else {
                    $where['pn.issue_id'] = isset($map['issue_id']) ? $map['issue_id'] : Db::name('IssueVersions')->order('ctime','desc')->value('id');
                    if ($issue_id) unset($where['pn.issue_id']);
                }
                if (isset($map['time'])) {
                    $where['pn.etime'][] = ['>=',strtotime(date('Y-m-d',$map['time'][0]) . ' 00:00:00')];
                    $where['pn.etime'][] = ['<=',strtotime(date('Y-m-d',$map['time'][1]) . ' 23:59:59')];
                }
                if (isset($map['status'])) $where['pn.status'] =  $map['status'];
                if (isset($map['type'])) $where['pn.type'] = $map['type'];
                if (isset($map['priority'])) $where['pn.priority'] = $map['priority'];
                if (isset($map['front_user'])) $where['pn.front_user'] = $map['front_user'];
                if (isset($map['after_user'])) $where['pn.after_user'] = $map['after_user'];
                if (isset($map['keywords'])) $like =  "pn.id like '%{$map['keywords']}%' OR pn.name like '%{$map['keywords']}%' OR pn.describe like '%{$map['keywords']}%' OR p.name like '%{$map['keywords']}%'";
                $field = 'pn.`id`,pn.`status`,pn.`issue_id`,pn.`type`,pn.`priority`,pn.`front_user`,pn.`after_user`,pn.`product_id`,pn.`source`,pn.`describe`,pn.`creator`,pn.`name`,pn.`ctime` as `ptime`,pn.`front_predict`,pn.`after_predict`,pn.`front_plan`,pn.`after_plan`,pn.`isup`,pn.`front_adjust`,pn.`after_adjust`,pn.`block_id`,pn.`etime`';
                $data = Db::name($this->hsModel)
                    ->alias('pn')
                    ->join('Project p','pn.product_id = p.id','left')
                    ->where($where)
                    ->where('pn.deleted','null')
                    ->where($like)
                    ->field($field)
                    ->order('pn.ctime', 'desc')
                    ->paginate([
                        'page' => $page,
                        'list_rows' => $limit,
                    ], false)
                    ->toArray();//dump($data['data']);die;

                $projectName = Db::name('project')->column('name','id');
                $block = Db::name('block')->where('deleted',null)->column('name','id');
                $deploy = Db::name('deploy')->where('deleted',null)->column('name','id');
                $user = Db::name('user')->where('deleted',0)->column('name','id');
                if (USER_DEPT == 0) {
                    $prefix = 'front_';
                } else if (USER_DEPT == 1) {
                    $prefix = 'after_';
                }
                foreach ($data['data'] as $key => &$value) {
                    $value['status'] = $this->statusCode[$value['status']];
                    //$value['project_id'] = $projectName[$value['project_id']];
                    $value['product_id'] = $projectName[$value['product_id']];
                    $value['block_id'] = $block[$value['block_id']];
                    $value['type'] = $deploy[$value['type']];
                    $value['priority'] = $deploy[$value['priority']];
                    $value['creator'] = $user[$value['creator']];
                    $value['front_user'] = $value['front_user'] ? $user[$value['front_user']] : '';
                    $value['after_user'] = $value['after_user'] ? $user[$value['after_user']] : '';
                    $value['ptime'] = $value['ptime'] ? date('Y-m-d',$value['ptime']) : '';
                    $value['etime'] = $value['etime'] ? date('Y-m-d',$value['etime']) : '';
                }
                if (isset($prefix)) {
                    foreach ($data['data'] as $key => &$value) {
                        $value['isshow'] = $value[$prefix.'predict'] > 0 ? false : true;
                        $value['isself'] = $value[$prefix.'user'] == USER_NAME ? true : false;
                        $value['isfinish'] = $value[$prefix.'plan'] == 100 ? false : true;
                    }
                }
            }
            return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
        }
    }

    /**
     * @title 获取首页需求数据
     * @description 接口说明
     * @author jyh
     * @url /ProjectBug/getIndexNeed
     * @method POST
     */
    public function getIndexNeed()
    {
        $db = Db::name($this->hsModel);
        $db = Db::name($this->hsModel);
        if (in_array(USER_DEPT,[0,1])) {
            $db = $db->where('status=1 or status=2');
        } else {
            $db = $db->where('status=3 or status=4 or status=5');
        }
        // 判断前端或后端加此条件
        if (USER_DEPT == 0 || USER_DEPT == 1) {
            $db = $db->where('front_user=' . USER_ID . ' or after_user=' . USER_ID);
        }
        // 查询出需求不等于0和6的数据
        $data = $db->where('deleted',null)->order('ctime','desc')->select();
        $projectName = Db::name('project')->column('name','id');
        $deploy = Db::name('deploy')->where('deleted',null)->column('name','id');
        $user = Db::name('user')->where('deleted',0)->column('name','id');
        foreach ($data as $key => $value) {
            $data[$key]['status'] = \app\home\model\ProjectNeed::$statusCode[$value['status']];
            $data[$key]['product_id'] = $projectName[$value['product_id']];
            $data[$key]['priority'] = $deploy[$value['priority']];
            $data[$key]['source'] = $deploy[$value['source']];
            $data[$key]['front_user'] = $value['front_user'] ? $user[$value['front_user']] : '';
            $data[$key]['after_user'] = $value['after_user'] ? $user[$value['after_user']] : '';
            $data[$key]['etime'] = $value['etime'] ? date('Y-m-d',$value['etime']) : '';
        }
        if ( empty($data) ) {
            return json(['code' => 0,'msg' => '暂无数据']);
        }
        return json(['code' => 0, 'msg' => '成功','data' => $data]);
    }


    /**
     * @title 新增需求
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/add
     * @method POST
     * @param name:project_id type:int require:1 default: other: desc:项目id
     * @param name:product_id type:int require:1 default: other: desc:产品id
     * @param name:versions_id type:int require:1 default: other: desc:版本id
     * @param name:block_id type:int require:1 default: other: desc:模块id
     * @param name:child_block type:string require:1 default: other: desc:子模块
     * @param name:name type:string require:1 default: other: desc:功能名称
     * @param name:type type:int require:1 default: other: desc:需求类型
     * @param name:source type:string require:1 default: other: desc:需求来源
     * @param name:priority type:int require:1 default: other: desc:优先级
     * @param name:describe type:string require:1 default: other: desc:需求描述
     */
    public function add()
    {
        $projectNeed = new \app\home\model\ProjectNeed();
        // 模型事件 在插入前执行图片插入
        $projectNeed::event('after_insert', function ($result) {
            $id = $result->id;
            $this->deal_upload($id);
        });
        return parent::add();
    }
    /**
     * @title 处理上传图片
     * @description 接口说明
     */
    public function deal_upload($id) {
        $path = "hs_system/need/{$id}/";
        $img = new \myclass\Imageup($path);
        $file = request()->file('file');
        if (!empty($file)) {
            $add_data = [];
            foreach ($file as $k=>$v) {
                $temp = $v->getInfo();
                if (strpos($temp['type'],'image') !== false) {
                    // 上传的图片
                    $result = $img->upload_img($v,0,1);
                } else {
                    // 其他文件
                    $result = $img->upload_doc($v,0,1);
                }
                if ($result['code'] == 1) {
                    return $result;
                }
                $temp['name'] = str_replace('&', '', $temp['name']);
                $temp['name'] = str_replace('=', '', $temp['name']);
                $add_data[] = [
                    'name' => $temp['name'],
                    'needs_id' => $id,
                    'url' => $result['url'],
                    'addtime' => time(),
                    'type' => $temp['type']
                ];
            }
            //录库
            $needInfo = new NeedInfo();

            $res = $needInfo->where('id', $id)->insertAll($add_data);
            if ($res) {
                return ['code'=>0,'msg'=>'上传成功','data'=>$add_data];
            }
        }
    }

    /**
     * @title 获取之前进度
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/getPlan
     * @method POST
     * @param name:need_id type:int require:1 default: other: desc:需求id
     */
    public function getPlan()
    {
        if ($this->request->isPost()) {
            $planModel = new RecordPlan();
            $params = $this->request->post();

            if (!isset($params['id'])) {
                return json(['code' => 1, 'msg' => '缺少必要参数']);
            }
            // 取值条件
            $where = [];
            $where['need_id'] = $params['id'];
            $where['user_id'] = USER_ID;
            $where['status'] = 1;
            // 取出之前进度的总和
            $plan = $planModel->where($where)->sum('plan');

            if ($plan) {
                return json(['code' => 0, 'msg' => 'ok','plan'=>$plan]);
            } else {
                return json(['code' => 1, 'msg' => 'error']);
            }
        }
    }

    /**
     * @title 更新进度
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/editPlan
     * @method POST
     */
    public function editPlan()
    {
        if ($this->request->isPost()) {

            $planModel = new RecordPlan();
            $params = $this->request->post();
            // 当天开始时间戳
            $dayStart = strtotime(date('Y-m-d', time()));
            // 当天结束时时间戳
            $dayEnd = $dayStart + 24 * 3600;

            if (!isset($params['id'])) {
                return json(['code' => 1, 'msg' => '缺少必要参数']);
            }
            // 查询条件
            $where = [];
            $where['need_id'] = $params['id'];
            $where['user_id'] = USER_ID;
            $where['ctime'][] = ['>', $dayStart];
            $where['ctime'][] = ['<', $dayEnd];

            $need = Db::name('projectNeed')->where('id', $params['id'])->find();
            $is_plan = $planModel->where($where)->find();
            // 启动事务
            Db::startTrans();
            try {
                if ($is_plan != false) {
                    // 把今天这个需求进度状态改为0
                    $planModel->save(['status' => 0], $where);
                    // 把之前这个需求最新进度改为0
                    $planModel->save(['newPlan' => 0], ['need_id' => $params['id'], 'user_id' => USER_ID]);
                }
                // 取出历史的进度总和
                $historyPlan = $planModel->where(['need_id' => $params['id'], 'user_id' => USER_ID, 'status' => 1])->sum('plan');
                // 插入新的进度
                $recordPlan = [];
                $recordPlan['need_id'] = $params['id'];
                $recordPlan['creator'] = USER_ID;
                $recordPlan['user_id'] = USER_ID;
                $recordPlan['plan'] = $params['plan'] - $historyPlan;
                $recordPlan['status'] = 1;
                $recordPlan['ctime'] = time();
                $res = $planModel->insert($recordPlan);
                if (!$res) {
                    return json(['code' => 1, 'msg' => '更新失败']);
                }
                // 判断是前端还是后端操作
                $prefix = USER_DEPT == 0 ? 'front_' : 'after_';
                $suffix = USER_DEPT == 1 ? 'front_' : 'after_';
                $after = $prefix == 'front_' ? 'after_user' : 'front_user';
                $nowData[$prefix . 'plan'] = $params['plan'];
                // 当前进度为100 记录工时
                if ($params['plan'] == 100) {
                    $nowData[$prefix . 'manhour'] = $need[$prefix . 'predict'];
                }
                // 进度等于一百就将状态改修改
                if ($need[$after] == 0 && $params['plan'] >= 100 && $need['task_allot'] != 4) {
                    $nowData['status'] = $params['status'];
                } else if ($nowData[$prefix . 'plan'] >= 100 && $need[$suffix . 'plan'] >= 100 && $need['task_allot'] != 4) {
                    $nowData['status'] = $params['status'];
                }
                $this->recordLog($params['id'],$params);
                $result = $this->model()->allowField(true)->save($nowData, ['id' => $params['id']]);
                if ($result) {
                    // 提交事务
                    Db::commit();
                    return json(['code' => 0, 'msg' => '更新完成']);
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                //return json(['code' => 1, 'msg' => '更新失败']);
                return json(['code' => 1, 'msg' => $e->getMessage(),'line' => $e->getLine()]);
            }
        }
    }

    /**
     * @title 需求领取池数据
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/needPond
     * @method POST
     */
    public function needPond()
    {
        if ( $this->request->isPost() ) {
            // 有$map条件 就是筛选
            $map = $this->request->post();
            $page = (int)input('page', 1);
            $limit = (int)input('limit', 10);
            $where = [];
            $like = '';
            foreach ($map as $key => $value) {
                if ($value === '') unset($map[$key]);
            }
            $where['pn.task_allot'] = ['<>',0];
            if (isset($map['project_id'])) $where['pn.project_id'] = $map['project_id'];
            if (isset($map['product_id'])) $where['pn.product_id'] = $map['product_id'];
            if (isset($map['time'])) {
                $where['pn.ctime'][] = ['>=',$map['time'][0]];
                $where['pn.ctime'][] = ['<=',$map['time'][1]];
            }
            if (isset($map['type'])) $where['pn.type'] = $map['type'];
            if (isset($map['priority'])) $where['pn.priority'] = $map['priority'];
            if (isset($map['keywords'])) $like =  "pn.id like '%{$map['keywords']}%' OR pn.name like '%{$map['keywords']}%' OR pn.describe like '%{$map['keywords']}%' OR b.name like '%{$map['keywords']}%' OR d.name like '%{$map['keywords']}%'";
            // 取出需求数据
            $data = $this->model()
                ->alias('pn')
                ->join('Project p','pn.project_id = p.id','left')
                ->join('Block b','pn.block_id = b.id','left')
                ->join('Deploy d','pn.source = d.id','left')
                ->field('pn.`id`,pn.`status`,p.`name` `project_id`,pn.`product_id`,b.`name` as block_id,pn.`type`,pn.`source`,pn.`priority`,pn.`name`,pn.`task_allot`,pn.`creator`,pn.`ctime`,pn.`front_predict`,pn.`after_predict`,pn.`task_allot`,pn.`block_id`')
                ->where($where)
                ->where($like)
                ->order('pn.ctime', 'desc')
                ->paginate([
                    'page' => $page,
                    'list_rows' => $limit,
                ], false);

            foreach ($data as $key => $value) {
                $data[$key]['front_task'] = $value['front_predict'] > 0 ? '已领取' : '未领取';
                $data[$key]['after_task'] = $value['after_predict'] > 0 ? '已领取' : '未领取';
            }

            if (empty($data)) {
                return json(['code' => 0, 'msg' => '暂无数据']);
            }
            return json(['code' => 0, 'msg' => '成功', 'data' => $data,'role' => USER_DEPT]);
        }
        return json(['code' => 0, 'msg' => '请求有误，请重新确认请求！']);
    }

    /**
     * @title 删除需求
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/del/:id
     * @method POST
     * @param name:ids type:int require:1 default: other: desc:需求ids
     */
    public function del($ids = "")
    {
        return parent::del($ids); // TODO: Change the autogenerated stub
    }

    /**
     * @title 修改需求估时
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/editTime/:id
     * @method POST
     * @param name:id type:int require:1 default: other: desc:需求id
     */
    public function editTime($id = "")
    {
        if ($this->request->isPost()) {
            $needModel = $this->model();
            // 查询本条需求数据
            $data = Db::name('ProjectNeed')->where('id',$id)->find();
            if (!$data['id']) {
                return json(['code' => 1, 'msg' => '编辑数据不存在']);
            }
            $params = $this->request->post();
            // 判断否为前后端 0=前端 1=后端
            if (USER_DEPT == 0) {
                $prefix = 'front_';
            } else if (USER_DEPT == 1) {
                $prefix = 'after_';
            } else {
                return json(['code' => 1, 'msg' => '参数错误']);
            }
            $nowData = [];
            // 判断进度值是否存在
            if (isset($params['predict'])) {
                $nowData[$prefix.'predict'] = $params['predict'];
                $nowData['status'] = $params['status'];
            }
            // 设置是否上线状态
            if (isset($params['status']) && $params['status'] == 6) {
                $after = USER_DEPT == 0 ? 'after_user' : 'front_user';
                if ($data[$after] == 0) {
                    $nowData['isup'] = 2;
                    $nowData['status'] = 6;
                } else if ($data['isup'] == 3) {
                    $nowData['isup'] = USER_DEPT;
                } else{
                    $nowData['isup'] = 2;
                    $nowData['status'] = 6;
                }
            }
            // 记录修改日记
            $this->recordLog($id,$params);
            $result = $needModel->allowField(true)->save($nowData,['id' => $id]);
            if ($result) {
                return json(['code' => 0, 'msg' => '编辑成功']);
            } else {
                return json(['code' => 1, 'msg' => '编辑失败']);
            }
        }
    }

    /**
     * @title 修改需求
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/edit/:id
     * @method POST
     * @param name:id type:int require:1 default: other: desc:需求id
     */
    public function edit($ids = null)
    {
        $needModel = $this->model();
        //查出修改前数据
        $data = Db::name('ProjectNeed')->where('id',$ids)->find();
        $params = $this->request->post();
        // 修改开发人员清除数据
        if ((isset($params['front_user']) && $data['front_user'] != $params['front_user']) || isset($params['task_allot'])) {
            $frontData = ['front_predict' => 0,'front_manhour' => 0,'front_plan' => 0];
            $needModel->allowField(true)->save($frontData,['id' => $ids]);
            Db::name('RecordPlan')->where(['need_id' => $ids,'user_id'=> $data['front_user']])->delete();
        }
        if ((isset($params['after_user']) && $data['after_user'] != $params['after_user']) || isset($params['task_allot'])) {
            $afterData = ['after_predict' => 0,'after_manhour' => 0,'after_plan' => 0];
            $needModel->allowField(true)->save($afterData,['id' => $ids]);
            Db::name('RecordPlan')->where(['need_id' => $ids,'user_id'=> $data['after_user']])->delete();
        }
        //查询更新前数据
        $needFile = Db::name('NeedInfo')->where('needs_id',$ids)->select();
        // 图片上传
        $upload = $this->deal_upload($ids);
        if ($upload['code'] == 0) {
            $params['file'] = $upload['data'];
        } else {
            return json($upload);
        }
        // 记录修改日记
        $this->recordLog($ids,$params,$needFile);
        return parent::edit($ids);
    }

    /**
     * @title 领取需求
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/getTask
     * @method POST
     * @param name:id type:int require:1 default: other: desc:需求id
     * @param name:predict type:int require:1 default: other: desc:开发估时
     */
    public function getTask()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            // 获取到前后端字段
            if (USER_DEPT == 0) {
                $prefix = 'front_';
                $front = 'front_user';
                $after = 'after_user';
            } else {
                $prefix = 'after_';
                $front = 'after_user';
                $after = 'front_user';
            }
            $map = [];
            if (!$params['id']) {
                return json(['code' => 1, 'msg' => '编辑数据不存在']);
            }
            // 查询该需求任务分配值
            $data = Db::name('ProjectNeed')->where('id',$params['id'])->find();
            $map[$front] = USER_ID;
            $map[$prefix.'predict'] = $params['predict'];
            $map['task_allot'] = 0;
            $map['status'] = 2;
            if ($data[$after] == 0 && in_array($data['task_allot'],[3,4])) {
                $map['task_allot'] = 4;
            } else {
                $map['task_allot'] = 0;
            }
            if (isset($params['user'])) {
                $map[$after] = $params['user'];
                $map['task_allot'] = 0;
            }
            $needFile = Db::name('NeedInfo')->where('needs_id',$params['id'])->select();
            $this->recordLog($params['id'],$map,$needFile);
            $result = $this->model()->allowField(true)->save($map,['id' => $params['id']]);
            if ($result) {
                return json(['code' => 0, 'msg' => '编辑成功']);
            } else {
                return json(['code' => 1, 'msg' => '编辑失败']);
            }
        }
    }

    /**
     * @title 删除需求图片
     * @description 接口说明
     * @author jyh
     * @url /projectNeed/delImg
     * @method POST
     * @param name:id type:int require:1 default: other: desc:需求图片id
     */
    public function delImg()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();

            if (!isset($params['id'])) {
                return json(['code' => 1, 'msg' => '缺少必要参数']);
            }
            // 取出当前图片信息
            $needFile = Db::name('NeedInfo')->where('id',$params['id'])->find();
            // 查询id不等于当前删除的图片
            $needFileAll = Db::name('NeedInfo')->where('needs_id',$needFile['needs_id'])->where('id','<>',$params['id'])->select();
            $needFileAll = array_column($needFileAll,'name');
            // 记录日记
            $log['uid'] = USER_ID;
            $log['uname'] = USER_NAME;
            $log['need_id'] = $needFile['needs_id'];
            $log['title'] = '附件';
            $log['mfront'] = $needFile['name'];
            $log['mafter'] = implode(',',$needFileAll);
            $log['ctime'] = time();

            $logModel = new SystemLog();
            $logModel->insert($log);
            $result = NeedInfo::where('id',$params['id'])->delete();
            if ($result > 0) {
                return json(['code' => 0, 'msg' => '删除成功']);
            } else {
                return json(['code' => 1, 'msg' => '删除失败']);
            }
        }
    }

    /**
     * @title 记录需求日记
     * @description 接口说明
     * @author jyh
     */
    protected function recordLog($id,$nowData,$needFile = [])
    {
        $data = Db::name('ProjectNeed')->where('id',$id)->find();
        if (isset($nowData['etime'])) {
            $data['etime'] = $data['etime'] ? date('Y-m-d',$data['etime']) : '';
            $nowData['etime'] = $data['etime'] ? date('Y-m-d',$nowData['etime']) : '';
        }
        if (isset($nowData['ptime'])) {
            $data['ptime'] = $data['ptime'] ? date('Y-m-d',$data['ptime']) : '';
            $nowData['ptime'] = $nowData['ptime'] ? date('Y-m-d',$nowData['ptime']) : '';
        }
        $statusName = [2 => '确认', 3 => '开发完成', 6 => '已上线'];
        if (isset($nowData['status']) && in_array($nowData['status'],[2,3,6])) {
            $this->projectName['status']['name'] = $statusName[$nowData['status']];
        } else {
            unset($this->projectName['status']);
        }
        $prefix = USER_DEPT == 0 ? 'front_' : 'after_';
        $data['plan'] = $data[$prefix.'plan'];
        if (isset($nowData['status']) && $nowData['status'] == $data['status']) {
            $data['status'] = $data['status']-1;
        }
        $log = [];
        foreach ($this->projectName as $key => $value) {
            if (!isset($nowData[$key])) continue;
            if ($nowData[$key] != $data[$key]) {
                $log[$key]['uid'] = USER_ID;
                $log[$key]['uname'] = USER_NAME;
                $log[$key]['need_id'] = $id;
                $log[$key]['title'] = $value['name'];
                $log[$key]['mfront'] = $value['table'] == '' ? $data[$key] : Db::name($value['table'])->where('id',$data[$key])->value('name');
                $log[$key]['mafter'] = $value['table'] == '' ? $nowData[$key] : Db::name($value['table'])->where('id',$nowData[$key])->value('name');
                $log[$key]['ctime'] = time();
                $log[$key]['option'] = isset($value['option']) ? $value['option'] : 0;
                $log[$key]['status'] = 0;
                if (in_array($key,['front_adjust','after_adjust'])) {
                    $log[$key]['mfront'] .= '小时';
                    $log[$key]['mafter'] .= '.0小时';
                }
            }
        }
        if (isset($nowData['file'])) {
            $needFileName = array_column($needFile,'name');
            $nowDataFile = array_column($nowData['file'],'name');

            $temp = false;
            foreach ($nowDataFile as $key => $value) {
                foreach ($needFileName as $k => $v) {
                    if ($value != $v) $temp = true;
                }
            }
            if ($needFileName == []) {
                $temp = true;
            }
            if ($temp) {
                $log['file']['uid'] = USER_ID;
                $log['file']['uname'] = USER_NAME;
                $log['file']['need_id'] = $id;
                $log['file']['title'] = '附件';
                $log['file']['mfront'] = $needFileName != [] ? implode(',',$needFileName) : "";
                $log['file']['mafter'] = $log['file']['mfront'] == "" ? implode(',',$nowDataFile) : $log['file']['mfront'] . ',' . implode(',',$nowDataFile);
                $log['file']['ctime'] = time();
                $log['file']['option'] = 0;
                $log['file']['status'] = 0;
            }
        }
        $logModel = new SystemLog();
        $log = array_values($log);
        if (!empty($log)) {
            $logModel->insertAll($log);
        }
    }
}